package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.*;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {



    @Autowired
    private ClazzMapper clazzMapper;

    @Override
    public PageResult page(ClazzQueryParam c) {

        //在查询前调用startPage
        PageHelper.startPage(c.getPage(), c.getPageSize());
        //执行查询,此时Mybatis返回的List已经是物理分页后的结果,mapper层
        List<Clazz> empList = clazzMapper.list(c);
        // 封装分页结果
        Page<Clazz> p = (Page<Clazz>) empList;
        return new PageResult(p.getTotal(), p.getResult());

    }

    @Override
    public void deleteClass(Integer id) {
        clazzMapper.deleteClass(id);
    }

    @Override
    public Clazz queryById(Integer id) {
        return clazzMapper.queryById(id);
    }

    @Override
    public void modifyClass(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.modifyClass(clazz);
    }

    @Override
    public List<Clazz> queryAllClasses() {
        return clazzMapper.queryAllClasses();
    }

    @Override
    public void save(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.save(clazz);
    }
}
